using Xamarin.Forms;

namespace Outcoder.UI.Xaml.Resources
{
	public class ImageUrlTransformer : IImageUrlTransformer
	{
		public string TransformForCurrentPlatform(string url)
		{
			string result = ArgumentValidator.AssertNotNull(url, "url");

			if (Device.OS == TargetPlatform.Android || Device.OS == TargetPlatform.iOS)
			{
				const string filePrefix = "file:///";
				if (url.StartsWith(filePrefix))
				{
					result = url.Substring(filePrefix.Length);
				}

				result = result.Replace("/", "_").Replace("\\", "_");
				if (result.StartsWith("_") && result.Length > 1)
				{
					result = result.Substring(1);
				}
			}
			else if (Device.OS == TargetPlatform.WinPhone)
			{
				if (url.StartsWith("/") && url.Length > 1)
				{
					result = result.Substring(1);
				}
			}

			return result;
		}
	}
}